package com.hellomeng.gmall.logger.controller;

import com.alibaba.fastjson.JSONObject;
import com.hellomeng.common.constant.GmallConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class LoggerController {
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @PostMapping("/log")
    public String log(@RequestParam("logString") String logString){
        JSONObject jsonObject = JSONObject.parseObject(logString);
        jsonObject.put("ts",System.currentTimeMillis());
        log.info(logString);

        //产生的日志信息直接怼给kafka
        if ("startup".equals(jsonObject.get("type"))){
            //启动日志
            kafkaTemplate.send(GmallConstant.KAFKA_STARTUP,jsonObject.toJSONString());
        }else {
            //事件日志
            kafkaTemplate.send(GmallConstant.KAFKA_EVENT,jsonObject.toJSONString());
        }
        return "success";
    }
}
